﻿using UnityEngine.Helper;

namespace UnityEngine.UI
{
    [AddComponentMenu("UI/DynamicImage")]
    public class DynamicImage : Image
    {
        public DynamicAtlasGroup mGroup = DynamicAtlasGroup.Size_1024;
        [SerializeField]
        public string spritePath = null;
        private DynamicAtlas m_Atlas;
        private OnCallBack onCallBack;
        private bool isSetImage = false;
        private Sprite defaultSprite;
        protected override void Start()
        {
            base.Start();
#if UNITY_EDITOR
            if (Application.isPlaying)
            {
                OnPreDoImage();
            }
#else
       OnPreDoImage();
#endif
        }
        void OnPreDoImage()
        {
            if (mainTexture != null && isSetImage == false) //说明这个rawimage事先挂了texture，此时需要给copy到图集中
            {
                SetGroup(mGroup);
                SetImage();
            }
        }
        public void SetGroup(DynamicAtlasGroup group)
        {
            if (m_Atlas != null)
            {
                return;
            }
            mGroup = group;
            m_Atlas = DynamicAtlasManager.Instance.GetDynamicAtlas(group);
        }

        public DynamicAtlasGroup GetGroup()
        {
            return mGroup;
        }
        public void OnGetImageCallBack(Texture tex, Rect rect, string path)
        {
            int length = (int)mGroup;
            Rect spriteRect = rect;
            spriteRect.x *= length;
            spriteRect.y *= length;
            spriteRect.width *= length;
            spriteRect.height *= length;
            //Create(Texture2D texture, Rect rect, Vector2 pivot, float pixelsPerUnit, uint extrude, SpriteMeshType meshType, Vector4 border);
            if(defaultSprite)
                sprite = Sprite.Create((Texture2D)tex, spriteRect, defaultSprite.pivot, defaultSprite.pixelsPerUnit, 1, SpriteMeshType.Tight, defaultSprite.border);
            else
                sprite = Sprite.Create((Texture2D)tex, spriteRect, new Vector2(0.5f,0.5f));
            gameObject.SetActiveVirtual(true);

            if (onCallBack != null)
            {
                onCallBack();
            }
        }
        private void SetImage()
        {
            defaultSprite = sprite;
            if (string.IsNullOrEmpty(spritePath))
            {
                return;
            }
            if (AtlasConfig.kUsingCopyTexture)//Sprite 只支持using copytexture
            {
                m_Atlas.SetTexture(spritePath, mainTexture, OnGetImageCallBack);
            }
            else
            {
                gameObject.SetActiveVirtual(false);
            }

            //此时可以卸载自己的引用计数
        }
        public void SetImage(string path, OnCallBack callBack = null)
        {
            isSetImage = true;
            gameObject.SetActiveVirtual(false);
            onCallBack = callBack;
            spritePath = path;
            if (m_Atlas == null)
            {
                SetGroup(mGroup);
            }
            m_Atlas.GetImage(path, OnGetImageCallBack);
        }
        public void SetTexture(string path, Texture2D texture2D)
        {
            spritePath = path;
            if (m_Atlas == null)
            {
                SetGroup(mGroup);
            }
            m_Atlas.SetTexture(path,texture2D,OnGetImageCallBack);
        }
        public void SetImageNoHide(string path, OnCallBack callBack = null)
        {
            onCallBack = callBack;
            spritePath = path;
            if (m_Atlas == null)
            {
                SetGroup(mGroup);
            }
            m_Atlas.GetImage(path, OnGetImageCallBack);
        }
        public void OnDispose()
        {
            if (string.IsNullOrEmpty(spritePath) == false && m_Atlas != null)
            {
                m_Atlas.RemoveImage(spritePath, false);
                //m_Atlas.RemoveImage(this._currentPath, true);
                spritePath = null;
            }
            isSetImage = false;
        }
        protected override void OnDestroy()
        {
            base.OnDestroy();
            OnDispose();
        }
    }
}
